package tetris;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

/**
 * Creates a Tetris Main Menu window.
 * @author dejvid
 *
 */
public class GameMenu extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel ground;
	private JTextField text;
	private JButton btnStartGame;
	private JButton btnHighScores;
	private JButton btnExitGame;

	/**
	 * Creates Main Menu.
	 * Creates buttons: NEW GAME, HIGH SCORES, EXIT GAME and textField: TETRIS GAME.
	 */
	public GameMenu() {

		setTitle("Tetris");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setSize(300,600);
		setLocationRelativeTo(null);
			
		ground = new JPanel();
		ground.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				btnStartGame.setBackground(Color.ORANGE);
				btnHighScores.setBackground(Color.ORANGE);
				btnExitGame.setBackground(Color.ORANGE);
			}
		});
		ground.setBorder(new EmptyBorder(5, 5, 5, 5));
		ground.setBackground(Color.DARK_GRAY);
		ground.setLayout(null);
		ground.setDoubleBuffered(true);
		setContentPane(ground);
		
		text = new JTextField();
		text.setBounds(6, 18, 290, 74);
		text.setEditable(false);
		text.setFont(new Font("OCR A Std", Font.PLAIN, 36));
		text.setBackground(Color.DARK_GRAY);
		text.setForeground(Color.WHITE);
		text.setText("TETRIS GAME");
		text.setColumns(10);
		text.setBorder(null);
		text.setFocusable(false);
		text.setHorizontalAlignment(JTextField.CENTER);
		text.setDoubleBuffered(true);
		ground.add(text);
		btnStartGame = new JButton("START GAME");
		btnStartGame.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				btnStartGame.setBackground(Color.RED);
			}
		});
		btnStartGame.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if( e.getKeyCode() == 's' || e.getKeyCode() == 'S')
					startGame();
				if( e.getKeyCode() == 'h' || e.getKeyCode() == 'H')
					viewHighScores();
				if( e.getKeyCode() == 'q' || e.getKeyCode() == 'Q')
					exitGame();
			}
		});
		btnStartGame.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				startGame();
			}
		});
		btnStartGame.setBackground(Color.ORANGE);
		btnStartGame.setFont(new Font("Courier", Font.BOLD, 23));
		btnStartGame.setBounds(72, 132, 155, 43);
		btnStartGame.setOpaque(true);
		btnStartGame.setBorder(null);
		btnStartGame.setDoubleBuffered(true);
		ground.add(btnStartGame);
		btnHighScores = new JButton("HIGH SCORES");
		btnHighScores.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				btnHighScores.setBackground(Color.RED);
			}
		});
		btnHighScores.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				viewHighScores();
			}
		});
		btnHighScores.setOpaque(true);
		btnHighScores.setFont(new Font("Courier", Font.BOLD, 21));
		btnHighScores.setDoubleBuffered(true);
		btnHighScores.setBorder(null);
		btnHighScores.setBackground(Color.ORANGE);
		btnHighScores.setBounds(72, 214, 155, 43);
		ground.add(btnHighScores);
		
		btnExitGame = new JButton("EXIT GAME");
		btnExitGame.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				exitGame();
			}
		});
		btnExitGame.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				btnExitGame.setBackground(Color.RED);
			}
		});
		btnExitGame.setOpaque(true);
		btnExitGame.setFont(new Font("Courier", Font.BOLD, 23));
		btnExitGame.setDoubleBuffered(true);
		btnExitGame.setBorder(null);
		btnExitGame.setBackground(Color.ORANGE);
		btnExitGame.setBounds(72, 295, 155, 43);
		ground.add(btnExitGame);
		
	}
	
	/**
	 * Runs game after clicking on NEW GAME button.
	 */
	public void startGame() {
        InsertName in = new InsertName();
        in.setVisible(true);
        dispose();
	}
	
	/**
	 * Shows top ten results after clicking on HIGH SCORES button.
	 */
	public void viewHighScores() {
		HighScores h = new HighScores();
		h.setVisible(true);
		dispose();
	}
	
	/**
	 * Exits game after clicking EXIT GAME button.
	 */
	public void exitGame() {
		System.exit(0);
	}
	
	/**
	 * Make Main Menu visible.
	 */
	public static void run() {
		GameMenu frame = new GameMenu();
		frame.setVisible(true);
	}
	public static void main(String[] args) {
		run();
	}

}
